<template>
  <div class="add-person-container">
    <el-form :model="personForm" :rules="rules" ref="personForm" label-width="100px">
      <el-form-item label="员工姓名" prop="name">
        <el-input v-model.trim="personForm.name" placeholder="请输入员工姓名"></el-input>
      </el-form-item>
      
      <el-form-item label="员工邮箱" prop="email">
        <el-input v-model.trim="personForm.email" placeholder="请输入员工邮箱"></el-input>
      </el-form-item>
      
      <el-form-item label="性别" prop="sex">
        <el-select v-model="personForm.sex" placeholder="请选择性别">
          <el-option label="男" value="1"></el-option>
          <el-option label="女" value="0"></el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="孩子信息">
        <el-button type="primary" @click="addChild" size="small">添加孩子</el-button>
        <div v-for="(child, index) in personForm.children" :key="index" class="child-item">
          <el-card class="child-card">
            <div class="child-form">
              <el-form-item 
                :prop="'children.' + index + '.name'" 
                :rules="rules['childName']"
                label="孩子姓名"
                label-width="80px">
                <el-input v-model.trim="child.name" placeholder="请输入孩子姓名"></el-input>
              </el-form-item>
              
              <el-form-item 
                :prop="'children.' + index + '.birthDate'" 
                :rules="rules['childBirth']"
                label="出生日期"
                label-width="80px">
                <el-date-picker
                  v-model="child.birthDate"
                  type="date"
                  placeholder="选择出生日期"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
              
              <el-button 
                type="danger" 
                icon="el-icon-delete" 
                circle 
                @click="removeChild(index)"
                size="mini">
              </el-button>
            </div>
          </el-card>
        </div>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitForm('personForm')">保存</el-button>
        <el-button @click="resetForm('personForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  name: 'AddPerson',
  data() {
    return {
      personForm: {
        name: '',
        email: '',
        sex: '',
        children: []
      },
      rules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入员工邮箱', trigger: 'blur' },
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        childName: [
          { required: true, message: '请输入孩子姓名', trigger: 'blur' }
        ],
        childBirth: [
          { required: true, message: '请选择出生日期', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    addChild() {
      this.personForm.children.push({
        name: '',
        birthDate: ''
      });
    },
    removeChild(index) {
      this.personForm.children.splice(index, 1);
    },
    submitForm(formName) {
      console.log('this.personForm :>> ', this.personForm);
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res =  await request.post('/person/save', this.personForm)
          if (res.code === '200') {
              this.$notify.success('新增成功')
              // this.$refs[formName].resetFields()
            } else {
              this.$notify.error(res.msg)
            }
          // 这里处理提交逻辑
          console.log('提交的数据:', this.personForm);
          // this.$message.success('员工信息保存成功');
        } else {
          console.log('表单验证失败');
          this.$message.error('请完善必填信息');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.add-person-container {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.child-item {
  margin-top: 15px;
}

.child-card {
  position: relative;
}

.child-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 15px;
}

.child-form .el-form-item {
  margin-bottom: 0;
  flex: 1;
  min-width: 200px;
}

.child-form .el-button {
  align-self: flex-end;
  margin-bottom: 5px;
}

@media (max-width: 768px) {
  .child-form {
    flex-direction: column;
    align-items: stretch;
  }
  
  .child-form .el-form-item {
    min-width: 100%;
  }
  
  .child-form .el-button {
    align-self: center;
    margin-top: 10px;
  }
}
</style>